package config

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
	"os"
	"time"
)

type Conn struct {
	Primary *gorm.DB
}

var MysqlConn *Conn

func InitMysql() {
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
		logger.Config{
			SlowThreshold:             time.Second, // Slow SQL threshold（慢速SQL阈值）
			LogLevel:                  logger.Info, // Log level（日志级别）
			IgnoreRecordNotFoundError: true,        // Ignore ErrRecordNotFound error for logger
			ParameterizedQueries:      true,        // Don't include params in the SQL log（忽略记录器的ErrRecordNotFound错误）
			Colorful:                  true,        // Disable color(禁用颜色)设置彩色打印
		},
	)
	dsn := Config.Db.Primary.UserName + ":" + Config.Db.Primary.Password + "@tcp(" + Config.Db.Primary.Host + ":" + Config.Db.Primary.Port + ")/" + Config.Db.Primary.DbName + "?charset=utf8mb4&parseTime=True&loc=Local"
	primary, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: newLogger,
	})
	if err != nil {
		panic(err)
	}
	MysqlConn = &Conn{Primary: primary}
	fmt.Println("mysql 链接成功")
}
